Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check for event.preventDefault() before setting state #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chaance
Copy link
Collaborator

@chaance chaance commented Sep 26, 2022

If a user prevents a form input's default behavior, the onChange in getInputAttrs shouldn't set state internally. To fix this I call the user's handler first, check for event.defaultPrevented and then continue with the internal event handler.

src/index.tsx Outdated Show resolved Hide resolved
Co-authored-by: Michaël De Boey <[email protected]>
onChange?.(event);
if (!event.defaultPrevented) {
setDirty(true);
setValue(event.target.value);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is what we want if the change event is prevented - since that happens after the input value has been set in the DOM (at least for text and similar inputs - would need to look closer on radio/checkbox). If we don't set the controlled value we can get into weird UI states:

Screen Shot 2022-09-29 at 8 43 07 AM

Users can use onKeyDown currently to prevent typed characters.

I've been thinking for a while too that I'd like to take a stab at making this entire library uncontrolled and just always use inputEl.value, which would remove this setValue entirely. I think that's feasible and would get rid of the useState so might have the worlds tiniest perf improvement 🎻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants